package timing.ukulele.android;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;

import timing.ukulele.android.fragment.BluetoothFragment;

import static androidx.navigation.Navigation.findNavController;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        Fragment mMainNavFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        NavController navController = NavHostFragment.findNavController(mMainNavFragment);
        int currentFragmentId = navController.getCurrentDestination().getId();
        if (id == R.id.action_serialport) {
            if (currentFragmentId == R.id.BluetoothFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_BluetoothFragment_to_PrintFragment);
            } else if (currentFragmentId == R.id.PrintFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_PrintFragment_to_SerialPortFragment);
            } else if (currentFragmentId == R.id.TerminalFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_TerminalFragment_to_SerialPortFragment);
            }
            return true;
        } else if (id == R.id.action_bluetooth) {
            if (currentFragmentId == R.id.SerialPortFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_SerialPortFragment_to_BluetoothFragment);
            } else if (currentFragmentId == R.id.PrintFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_PrintFragment_to_BluetoothFragment);
            } else if (currentFragmentId == R.id.TerminalFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_TerminalFragment_to_BluetoothFragment);
            }
            return true;
        } else if (id == R.id.action_print) {
            if (currentFragmentId == R.id.BluetoothFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_BluetoothFragment_to_PrintFragment);
            } else if (currentFragmentId == R.id.SerialPortFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_SerialPortFragment_to_PrintFragment);
            } else if (currentFragmentId == R.id.TerminalFragment) {
                findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_TerminalFragment_to_PrintFragment);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}